/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-3-17 下午5:31
 */
package com.lenovo.mso2o.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.easemob.chat.EMMessage;
import com.easemob.chat.VoiceMessageBody;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.entity.FGson;
import com.lenovo.framework.entity.Res;

import java.io.File;

public class VoicePlayClickListener {
	private static final String TAG = "VoicePlayClickListener";
	EMMessage message;
	VoiceMessageBody voiceBody;

	private AnimationDrawable voiceAnimation = null;
	static MediaPlayer mediaPlayer = null;
	ImageView iv_read_status;
	private BaseAdapter adapter;

	public static boolean isPlaying = false;
	public static VoicePlayClickListener currentPlayListener = null;

	/**
	 * 
	 * @param message
	 * @param v
	 * @param iv_read_status
	 * @param activity
	 */
	public VoicePlayClickListener(EMMessage message, ImageView v, ImageView iv_read_status, BaseAdapter adapter, Activity activity,
								  String username) {
		this.message = message;
		voiceBody = (VoiceMessageBody) message.getBody();
		this.iv_read_status = iv_read_status;
		this.adapter = adapter;
	}

	public static void stopPlayVoiceFile(CallBackFunction callback) {
		// stop play voice
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
		}
		isPlaying = false;
        Res res = new Res();
        res.setStatus(1);
        callback.onCallBack(FGson.gson().toJson(res));
	}

	public static void playVoiceFile(String filePath, final CallBackFunction callback) {
		Res res = new Res();
		if (!(new File(filePath).exists())) {
			res.setStatus(0);
			callback.onCallBack(FGson.gson().toJson(res));
			return;
		}
		AudioManager audioManager = (AudioManager) ContextManager.context().getSystemService(Context.AUDIO_SERVICE);

		mediaPlayer = new MediaPlayer();
		//TODO
		//if (HXSDKHelper.getInstance().getModel().getSettingMsgSpeaker()) {
			audioManager.setMode(AudioManager.MODE_NORMAL);
			audioManager.setSpeakerphoneOn(true);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
		/*} else {
			audioManager.setSpeakerphoneOn(false);// 关闭扬声�?
			// 把声音设定成Earpiece（听筒）出来，设定为正在通话�?
			audioManager.setMode(AudioManager.MODE_IN_CALL);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
		*/
		try {
			mediaPlayer.setDataSource(filePath);
			mediaPlayer.prepare();
			mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mediaPlayer.release();
					mediaPlayer = null;
					stopPlayVoiceFile(callback); // stop animation
				}

			});
			isPlaying = true;
			mediaPlayer.start();
		} catch (Exception e) {
		}
	}
}